package fr.waitandsee.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;
import fr.waitandsee.FragmentController;
import fr.waitandsee.R;

public class SenderFragment extends Fragment implements PageFragmentModel {
	
	private static final String TAG = "SenderFragment";
	
	private FragmentController fragmentController;
	private Handler handler;
	
	private Button bPhoto;
	private Button bVideo;
	private Button bRec;
	private TextView tvBack;
	private TextView tvFacebook;
	
	public static SenderFragment newInstance(/*parameters*/) {
		SenderFragment fragment = new SenderFragment();
		// set parameters if needed : 
	    // Bundle bundle = new Bundle(1);
	    // bundle.putInt(EXTRA_NAME, param);
	    // fragment.setArguments(bundle);
	    return fragment ;
	}
	
	
	  /////////////////////////
	 // FRAGMENT LIFE CYCLE //
	/////////////////////////
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate.savedInstanceState="+savedInstanceState);
		this.fragmentController = (FragmentController) getActivity();
		this.handler = new Handler();
	}
	
 	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 		Log.d(TAG, "onCreateView.savedInstanceState="+savedInstanceState);
		if (savedInstanceState != null) {
			// get saved parameters if needed : 
			// param = savedInstanceState.getInt(EXTRA_NAME);
		} else {
			if(getArguments() != null) {
				// get parameters if needed :
				// param = getArguments().getInt(EXTRA_NAME);
			}
		}
		View v = inflater.inflate(R.layout.fragment_sender, container, false);
		return v;
	}
	
 	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
 		Log.d(TAG, "onViewCreated.savedInstanceState="+savedInstanceState);
		fetchViews();
		customizeViews();
		setListeners();
	}
 	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		Log.d(TAG, "onSaveInstanceState.outState="+outState);
		super.onSaveInstanceState(outState);
		// save parameters if needed :
		// outState.putInt(EXTRA_NAME, param);
	}
 	
	@Override
 	public void fetchViews() {
		this.bPhoto = (Button) getView().findViewById(R.id.button_photo);
		this.bVideo = (Button) getView().findViewById(R.id.button_video);
		this.bRec = (Button) getView().findViewById(R.id.button_rec);
		this.tvBack = (TextView) getView().findViewById(R.id.textView_back);
		this.tvFacebook = (TextView) getView().findViewById(R.id.textView_fb_invitation);
 	}
	
	@Override
	public void customizeViews() {
		
	}
 	
	@Override
	public void setListeners() {
		this.bPhoto.setOnClickListener(onPhotoClickListener);
		this.bVideo.setOnClickListener(onVideoClickListener);
		this.bRec.setOnClickListener(onRecClickListener);
		this.tvBack.setOnTouchListener(onBackTouchListener);
		this.tvFacebook.setOnTouchListener(onFacebookTouchListener);
 	}
	
	
	  ///////////////
	 // LISTENERS //
	///////////////
 	
 	private OnClickListener onPhotoClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Log.i(TAG, "onPhotoClickListener");
			//TODO action on "Photo" button clicked
		}
	};
	
 	private OnClickListener onVideoClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Log.i(TAG, "onVideoClickListener");
			//TODO action on "Video" button clicked
		}
	};
	
 	private OnClickListener onRecClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Log.i(TAG, "onRecClickListener");
			fragmentController.switchContent(RecorderFragment.newInstance());
		}
	};
	
	private OnTouchListener onBackTouchListener = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			Log.i(TAG, "onBackTouchListener");
			fragmentController.switchContent(HomeFragment.newInstance());
			return false;
		}
 	};
	
	private OnTouchListener onFacebookTouchListener = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			Log.i(TAG, "onFacebookTouchListener");
			tvFacebook.setOnTouchListener(null); // stand-by listener to prevent multi openning
			handler.postDelayed(delayAfterTouchCallback, 200); // re-set on touch listener after 200ms
			//TODO action on "facebook" textview touched
			return false;
		}
 	};

 	private Runnable delayAfterTouchCallback = new Runnable() {
 		@Override 
 		public void run() {
 			tvFacebook.setOnTouchListener(onFacebookTouchListener);
 			tvBack.setOnTouchListener(onBackTouchListener);
 		}
 	};
	
}
